
/**
 * BPMCaseItemLog
 *
 * new BPMCaseItemLog({
 *
 * });
 * ------------------------------------
 *
 * API:
 *-------------------------------------
 *
 *  fromXML(caseWorkitemLogXml)
 *  getWorkItems()
 *
 */
(function(global){

    'use strict';

    const logger = WorkflowUtil.newLogger("WorkflowApp/BPMCaseItemLog");

    let BPMCaseItemLog = function () {

        BPMCaseItemLog.__super__.constructor.apply(this, arguments);

        // itemLogList
        this.itemLogList = [];
        this._id2Item = {};
    };


    //继承
    WorkflowUtil.extend(BPMCaseItemLog, global.BPMObject);
    
    
    BPMCaseItemLog.prototype.fromXML = function(caseWorkitemLogXml){

        const root = WorkflowUtil.getXMLDoc(caseWorkitemLogXml);
        let child = root.documentElement.firstChild;

        while (child != null){

            const itemLog = new global.ItemLog();
            itemLog.fromXML(child);
            
            this.itemLogList.push(itemLog);
            
            
            // 通过ID归类
            let workitems = this._id2Item[itemLog.getNodeId];
            if(!workitems){
                workitems = [];
            }
            workitems.push(itemLog);
            this._id2Item[itemLog.getNodeId] = workitems;
            
            // 继续循环
            child = child.nextSibling;
        }
    };
    
    
    
    BPMCaseItemLog.prototype.getWorkItems = function(nodeId){
        
        return this._id2Item[nodeId] || [];
    };
    
    
    
    global.BPMCaseItemLog = BPMCaseItemLog;

})(WorkflowApp);



/**
 * API
 * --------------
 * fromXML(xmlDom)
 * getNodeId()
 * isRead():boolean
 */
(function(global){

    'use strict';

    const logger = WorkflowUtil.newLogger("WorkflowApp/ItemLog");

    let ItemLog = function () {

        ItemLog.__super__.constructor.apply(this, arguments);

        //初始化属性
        //this.addAttr("type", "", "t");

        this.addAttr("itemId", "", "I");
        this.addAttr("nodeId", "", "N");
        this.addAttr("batch", "", "B");
        this.addAttr("performer", "", "P");
        this.addAttr("performerName", "", "PN");
        this.addAttr("performerState", "", "PS");
        this.addAttr("accountShortName", "", "PA");
        this.addAttr("itemCreateDate", "", "ICD");
        this.addAttr("itemUpdateDate", "", "IUD");
        this.addAttr("actionState", "", "AS");


        // actionList
        this.actionList = [];
        this.isReadState = false;
    };

    //继承
    WorkflowUtil.extend(ItemLog, global.BPMObject);

    ItemLog.prototype.fromXML = function(xmlDom){
        
        this.transXMLNode2BPMObject(xmlDom);
        
     // 重置一下这些节点
        this.actionList = [];
        this.isReadState = false;

        const actionState = this.getAttr("actionState");

        if(actionState != null && actionState !== ""){

            const actionStates = actionState.split(",");
            for (let index = 0; index < actionStates.length; index++) {

                const action = actionStates[index];
                if(action == null || action === ""){
                    continue;
                }
                
                if(action === "26"){//26是已读
                    this.isReadState = true;
                }
                else{
                    this.actionList.push(action);
                }
            }
        } else{
            
            /*
             * 
             这里是以前的XML解析逻辑，现在的数据结构没有这种情况了
             
            var child:XMLNode = xml.firstChild;
            while (child != null) {
                var action:Action = new Action();
                action.fromXML(child);
                
                if(action.action == "26"){//26是已读
                    isReadState = true;
                }
                else{
                    actionList.push(action);
                }
                
                child = child.nextSibling;
            }*/
        }
    };
    
    
    ItemLog.prototype.getNodeId = function(){
        return this.getAttr("nodeId");
    };
    
    ItemLog.prototype.isRead = function(){
        return this.isReadState;
    };
    
    ItemLog.prototype.getItemAction = function(){
        
        if(this.actionList.length > 0){
            // 取最后一个action
            return this.actionList[this.actionList.length - 1];
        }
        
        return "";
    };
    
    global.ItemLog = ItemLog;

})(WorkflowApp);


